package com.navi168.fwq21.hyzl;

import java.util.List;

import android.content.Context;
import android.content.Intent;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.amap.mapapi.map.MapController;
import com.amap.mapapi.map.MapView;
import com.amap.mapapi.core.PoiItem;
import com.amap.mapapi.map.MapView.LayoutParams;
import com.amap.mapapi.map.PoiOverlay;
import com.navi168.fwq21.R;
import com.navi168.fwq21.hyzl.mod.MypoiItems;
import com.navi168.fwq21.util.SendPosition;

public class MyPoiOverlay extends PoiOverlay {
	private Context context;
	private Drawable drawable;
	private List<PoiItem> poiItem;
	private LayoutInflater mInflater;
	private int height;
	private MapController mMapController;
	private MapView mapView;
	private MypoiItems mypoiItems = new MypoiItems();

	public MyPoiOverlay(Context context, Drawable drawable,
			List<PoiItem> poiItem, MapView mapView) {
		super(drawable, poiItem);
		this.context = context;
		this.poiItem = poiItem;
		this.mapView = mapView;

		mInflater = LayoutInflater.from(context);
		height = drawable.getIntrinsicHeight();
		mMapController = mapView.getController();

	}

	@Override
	protected Drawable getPopupBackground() {
		// TODO Auto-generated method stub
		drawable = context.getResources().getDrawable(
				R.drawable.tip_pointer_button);
		return drawable;
	}

	@Override
	protected View getPopupView(final PoiItem item) {
		// TODO Auto-generated method stub

		final View view = mInflater.inflate(R.layout.popup, null);
		TextView nameTextView = (TextView) view.findViewById(R.id.PoiName);
		TextView addressTextView = (TextView) view
				.findViewById(R.id.PoiAddress);
		LinearLayout llLayout = (LinearLayout) view
				.findViewById(R.id.LinearLayoutPopup);
		ImageButton nearButton = (ImageButton) view.findViewById(R.id.button1);
		ImageButton xfButton = (ImageButton) view.findViewById(R.id.xf_button);
		nameTextView.setText(item.getTitle());
		String address = item.getSnippet();
		if (address == null || address.length() == 0) {
			address = "上海";
		}
		addressTextView.setText(address);
		LinearLayout layout = (LinearLayout) view
				.findViewById(R.id.LinearLayoutPopup);
		layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 实现onClick事件
				mapView.removeView(view);
				Log.i("mylog", item.getPoint().getLatitudeE6() + ":"
						+ item.getPoint().getLongitudeE6());
			}
		});

		Button.OnClickListener nearpoiListener = new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// 传送点击按钮位置的经纬度
				Log.i("mylog", "执行了but");
				Bundle bundle = new Bundle();
				bundle.putInt("lat", item.getPoint().getLatitudeE6());
				bundle.putInt("long", item.getPoint().getLongitudeE6());
				// bundle.putString("add_name", item.getTitle());
				bundle.putString("searchtype", item.getTitle());
				Intent it = new Intent();
				it.putExtras(bundle);
				it.setClass(context, NearPoi.class);
				context.startActivity(it);

			}
		};

		Button.OnClickListener xiafaListener = new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// 传送点击按钮位置的经纬度
				Log.i("mylog", "执行了 :" + item.getSnippet() + item.getTitle()
						+ item.getPoint().getLongitudeE6());

				mypoiItems.setSnippet(item.getSnippet());
				mypoiItems.setTitle(item.getTitle());
				mypoiItems.setLatit(item.getPoint().getLatitudeE6());
				mypoiItems.setLongit(item.getPoint().getLongitudeE6());
				Bundle bundle = new Bundle();
				bundle.putParcelable("poi_xf", mypoiItems);
				Intent it = new Intent();
				it.putExtra("poi_xf", bundle);
				it.setClass(context, PoiMenu.class);
				context.startActivity(it);

			}
		};
		nearButton.setOnClickListener(nearpoiListener);
		xfButton.setOnClickListener(xiafaListener);

		return view;
	}

	@Override
	public void addToMap(MapView arg0) {
		// TODO Auto-generated method stub
		super.addToMap(arg0);
	}

	@Override
	protected LayoutParams getLayoutParam(int arg0) {
		// TODO Auto-generated method stub
		LayoutParams params = new MapView.LayoutParams(
				MapView.LayoutParams.WRAP_CONTENT,
				MapView.LayoutParams.WRAP_CONTENT,
				poiItem.get(arg0).getPoint(), 0-12, -height,
				LayoutParams.BOTTOM_CENTER);
		return params;
	}

	@Override
	protected Drawable getPopupMarker(PoiItem arg0) {
		// TODO Auto-generated method stub
		return super.getPopupMarker(arg0);
	}

	@Override
	protected boolean onTap(int index) {
		Log.i("mylog", "ontap");
		mMapController.animateTo(poiItem.get(index).getPoint());
		return super.onTap(index);
	}

}
